package com.shujia.wyh.day26;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
    定义一个类实现Runnable接口
 */
public class TicketWindow3 implements Runnable {
    //定义100张票
    private int tickets = 100;
    //    private Object obj = new Object();
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (tickets > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在出售第" + (tickets--) + "张票。。");
            }
            lock.unlock();
        }
    }
}
